{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第一节 单因素方差分析"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 实验5-1 单因素方差分析"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "检查三家工厂的机器完成生产所需平均时间是否相同。三家工厂机器完成生产所需时间如下\n",
    "\n",
    "α=0.05\n",
    "\n",
    "H0:三家工厂的机器完成生产所需平均时间相同\n",
    "    \n",
    "H1:三家工厂的机器完成生产所需平均时间不同"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "import statsmodels.api as sm \n",
    "from statsmodels.formula.api import ols"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>20</td>\n",
       "      <td>28</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>26</td>\n",
       "      <td>26</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>24</td>\n",
       "      <td>31</td>\n",
       "      <td>23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>22</td>\n",
       "      <td>27</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    1   2   3\n",
       "0  20  28  20\n",
       "1  26  26  19\n",
       "2  24  31  23\n",
       "3  22  27  22"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "productivity_df = pd.read_excel('5-1.xlsx')\n",
    "productivity_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>factory</th>\n",
       "      <th>time_spent</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2</td>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2</td>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>3</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>3</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>3</td>\n",
       "      <td>23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>3</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    factory  time_spent\n",
       "0         1          20\n",
       "1         1          26\n",
       "2         1          24\n",
       "3         1          22\n",
       "4         2          28\n",
       "5         2          26\n",
       "6         2          31\n",
       "7         2          27\n",
       "8         3          20\n",
       "9         3          19\n",
       "10        3          23\n",
       "11        3          22"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将原始数据的宽表转化成便于数据分析的长表\n",
    "productivity_df_long = productivity_df.melt(var_name='factory' , value_name='time_spent')\n",
    "productivity_df_long"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"8\" halign=\"left\">time_spent</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>factory</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4.0</td>\n",
       "      <td>23.0</td>\n",
       "      <td>2.581989</td>\n",
       "      <td>20.0</td>\n",
       "      <td>21.50</td>\n",
       "      <td>23.0</td>\n",
       "      <td>24.50</td>\n",
       "      <td>26.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4.0</td>\n",
       "      <td>28.0</td>\n",
       "      <td>2.160247</td>\n",
       "      <td>26.0</td>\n",
       "      <td>26.75</td>\n",
       "      <td>27.5</td>\n",
       "      <td>28.75</td>\n",
       "      <td>31.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>1.825742</td>\n",
       "      <td>19.0</td>\n",
       "      <td>19.75</td>\n",
       "      <td>21.0</td>\n",
       "      <td>22.25</td>\n",
       "      <td>23.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        time_spent                                                \n",
       "             count  mean       std   min    25%   50%    75%   max\n",
       "factory                                                           \n",
       "1              4.0  23.0  2.581989  20.0  21.50  23.0  24.50  26.0\n",
       "2              4.0  28.0  2.160247  26.0  26.75  27.5  28.75  31.0\n",
       "3              4.0  21.0  1.825742  19.0  19.75  21.0  22.25  23.0"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用pandas中groupby按工厂进行分组，然后用describe方法对各组进行描述性统计\n",
    "productivity_df_long.groupby('factory').describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>df</th>\n",
       "      <th>sum_sq</th>\n",
       "      <th>mean_sq</th>\n",
       "      <th>F</th>\n",
       "      <th>PR(&gt;F)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>C(factory)</th>\n",
       "      <td>2.0</td>\n",
       "      <td>104.0</td>\n",
       "      <td>52.000000</td>\n",
       "      <td>10.636364</td>\n",
       "      <td>0.00426</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Residual</th>\n",
       "      <td>9.0</td>\n",
       "      <td>44.0</td>\n",
       "      <td>4.888889</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             df  sum_sq    mean_sq          F   PR(>F)\n",
       "C(factory)  2.0   104.0  52.000000  10.636364  0.00426\n",
       "Residual    9.0    44.0   4.888889        NaN      NaN"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "productivity_lm = ols('time_spent~C(factory)' , data=productivity_df_long).fit()\n",
    "sm.stats.anova_lm(productivity_lm)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "结论\n",
    "\n",
    "由于p值 0.00426 < 0.05 ，因此在5%显著性水平下，应拒绝原假设，即三个厂机器完成工作时间是有显著差异的"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第二节 双因素方差分析"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 实验5-2 无交互作用的双因素方差分析"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "三个品牌的手机在四个地区销售，销售数据如下。显著性水平5%，分析手机销售量是否由于品牌的不同和地区的不同而存在差异。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "import statsmodels.api as sm \n",
    "from statsmodels.formula.api import ols"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>地区1</th>\n",
       "      <th>地区2</th>\n",
       "      <th>地区3</th>\n",
       "      <th>地区4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>品牌1</th>\n",
       "      <td>4.5</td>\n",
       "      <td>6.4</td>\n",
       "      <td>7.2</td>\n",
       "      <td>6.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>品牌2</th>\n",
       "      <td>8.8</td>\n",
       "      <td>7.8</td>\n",
       "      <td>9.6</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>品牌3</th>\n",
       "      <td>5.9</td>\n",
       "      <td>6.8</td>\n",
       "      <td>5.7</td>\n",
       "      <td>5.2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     地区1  地区2  地区3  地区4\n",
       "品牌1  4.5  6.4  7.2  6.7\n",
       "品牌2  8.8  7.8  9.6  7.0\n",
       "品牌3  5.9  6.8  5.7  5.2"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sell_df = pd.read_excel('5-2.xlsx',index_col=0) # 注意索引\n",
    "sell_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>brand</th>\n",
       "      <th>地区1</th>\n",
       "      <th>地区2</th>\n",
       "      <th>地区3</th>\n",
       "      <th>地区4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>品牌1</td>\n",
       "      <td>4.5</td>\n",
       "      <td>6.4</td>\n",
       "      <td>7.2</td>\n",
       "      <td>6.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>品牌2</td>\n",
       "      <td>8.8</td>\n",
       "      <td>7.8</td>\n",
       "      <td>9.6</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>品牌3</td>\n",
       "      <td>5.9</td>\n",
       "      <td>6.8</td>\n",
       "      <td>5.7</td>\n",
       "      <td>5.2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  brand  地区1  地区2  地区3  地区4\n",
       "0   品牌1  4.5  6.4  7.2  6.7\n",
       "1   品牌2  8.8  7.8  9.6  7.0\n",
       "2   品牌3  5.9  6.8  5.7  5.2"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 清洗数据，变长表\n",
    "sell_df_n = sell_df.reset_index()\n",
    "sell_df_n = sell_df_n.rename(index = str , columns = {'index':'brand'})\n",
    "sell_df_n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>brand</th>\n",
       "      <th>area</th>\n",
       "      <th>sell</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>品牌1</td>\n",
       "      <td>地区1</td>\n",
       "      <td>4.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>品牌2</td>\n",
       "      <td>地区1</td>\n",
       "      <td>8.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>品牌3</td>\n",
       "      <td>地区1</td>\n",
       "      <td>5.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>品牌1</td>\n",
       "      <td>地区2</td>\n",
       "      <td>6.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>品牌2</td>\n",
       "      <td>地区2</td>\n",
       "      <td>7.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>品牌3</td>\n",
       "      <td>地区2</td>\n",
       "      <td>6.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>品牌1</td>\n",
       "      <td>地区3</td>\n",
       "      <td>7.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>品牌2</td>\n",
       "      <td>地区3</td>\n",
       "      <td>9.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>品牌3</td>\n",
       "      <td>地区3</td>\n",
       "      <td>5.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>品牌1</td>\n",
       "      <td>地区4</td>\n",
       "      <td>6.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>品牌2</td>\n",
       "      <td>地区4</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>品牌3</td>\n",
       "      <td>地区4</td>\n",
       "      <td>5.2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   brand area  sell\n",
       "0    品牌1  地区1   4.5\n",
       "1    品牌2  地区1   8.8\n",
       "2    品牌3  地区1   5.9\n",
       "3    品牌1  地区2   6.4\n",
       "4    品牌2  地区2   7.8\n",
       "5    品牌3  地区2   6.8\n",
       "6    品牌1  地区3   7.2\n",
       "7    品牌2  地区3   9.6\n",
       "8    品牌3  地区3   5.7\n",
       "9    品牌1  地区4   6.7\n",
       "10   品牌2  地区4   7.0\n",
       "11   品牌3  地区4   5.2"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sell_df_long = sell_df_n.melt(id_vars='brand' , var_name='area' , value_name='sell')\n",
    "sell_df_long"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>df</th>\n",
       "      <th>sum_sq</th>\n",
       "      <th>mean_sq</th>\n",
       "      <th>F</th>\n",
       "      <th>PR(&gt;F)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>C(brand)</th>\n",
       "      <td>2.0</td>\n",
       "      <td>13.68</td>\n",
       "      <td>6.840000</td>\n",
       "      <td>6.237082</td>\n",
       "      <td>0.034258</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C(area)</th>\n",
       "      <td>3.0</td>\n",
       "      <td>2.82</td>\n",
       "      <td>0.940000</td>\n",
       "      <td>0.857143</td>\n",
       "      <td>0.512185</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Residual</th>\n",
       "      <td>6.0</td>\n",
       "      <td>6.58</td>\n",
       "      <td>1.096667</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           df  sum_sq   mean_sq         F    PR(>F)\n",
       "C(brand)  2.0   13.68  6.840000  6.237082  0.034258\n",
       "C(area)   3.0    2.82  0.940000  0.857143  0.512185\n",
       "Residual  6.0    6.58  1.096667       NaN       NaN"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sell_lm = ols('sell~C(brand)+C(area)' , data=sell_df_long).fit()\n",
    "sm.stats.anova_lm(sell_lm) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "结论\n",
    "\n",
    "对于品牌因素 p值0.034258 < 0.05 ， 拒绝原假设，即品牌是重要的影响因素。\n",
    "\n",
    "对于地区因素 p值0.512185 > 0.05 ， 拒绝无法原假设，即品牌不是重要的影响因素。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 实验5-3 有交互作用的双因素方差分析"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "五一与十一两个黄金周，四个旅游线路，旅游情况如下，显著性水平5%\n",
    "\n",
    "判断：\n",
    "\n",
    "旅游线路之间是否存在差异？\n",
    "\n",
    "两个黄金周之间是否存在差异？\n",
    "\n",
    "是否存在线路与黄金周交互作用的影响？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "import statsmodels.api as sm \n",
    "from statsmodels.formula.api import ols"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>线路A</th>\n",
       "      <th>线路B</th>\n",
       "      <th>线路C</th>\n",
       "      <th>线路D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>五一</th>\n",
       "      <td>31</td>\n",
       "      <td>22</td>\n",
       "      <td>14</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NaN</th>\n",
       "      <td>29</td>\n",
       "      <td>23</td>\n",
       "      <td>16</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NaN</th>\n",
       "      <td>32</td>\n",
       "      <td>26</td>\n",
       "      <td>20</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NaN</th>\n",
       "      <td>30</td>\n",
       "      <td>25</td>\n",
       "      <td>15</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NaN</th>\n",
       "      <td>30</td>\n",
       "      <td>24</td>\n",
       "      <td>18</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>十一</th>\n",
       "      <td>25</td>\n",
       "      <td>21</td>\n",
       "      <td>16</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NaN</th>\n",
       "      <td>22</td>\n",
       "      <td>20</td>\n",
       "      <td>13</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NaN</th>\n",
       "      <td>27</td>\n",
       "      <td>16</td>\n",
       "      <td>15</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NaN</th>\n",
       "      <td>26</td>\n",
       "      <td>19</td>\n",
       "      <td>12</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NaN</th>\n",
       "      <td>22</td>\n",
       "      <td>15</td>\n",
       "      <td>10</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     线路A  线路B  线路C  线路D\n",
       "五一    31   22   14    8\n",
       "NaN   29   23   16    4\n",
       "NaN   32   26   20    6\n",
       "NaN   30   25   15    5\n",
       "NaN   30   24   18    5\n",
       "十一    25   21   16    5\n",
       "NaN   22   20   13    7\n",
       "NaN   27   16   15    8\n",
       "NaN   26   19   12    7\n",
       "NaN   22   15   10    8"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tourist_df = pd.read_excel('5-3.xlsx' , index_col=0) # 注意索引\n",
    "tourist_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\users\\xinzhi\\appdata\\local\\programs\\python\\python39\\lib\\site-packages\\pandas\\core\\indexing.py:719: FutureWarning: Slicing a positional slice with .loc is not supported, and will raise TypeError in a future version.  Use .loc with labels or .iloc with positions instead.\n",
      "  indexer = self._get_setitem_indexer(key)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>period</th>\n",
       "      <th>线路A</th>\n",
       "      <th>线路B</th>\n",
       "      <th>线路C</th>\n",
       "      <th>线路D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>五一</td>\n",
       "      <td>31</td>\n",
       "      <td>22</td>\n",
       "      <td>14</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>五一</td>\n",
       "      <td>29</td>\n",
       "      <td>23</td>\n",
       "      <td>16</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>五一</td>\n",
       "      <td>32</td>\n",
       "      <td>26</td>\n",
       "      <td>20</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>五一</td>\n",
       "      <td>30</td>\n",
       "      <td>25</td>\n",
       "      <td>15</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>五一</td>\n",
       "      <td>30</td>\n",
       "      <td>24</td>\n",
       "      <td>18</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>十一</td>\n",
       "      <td>25</td>\n",
       "      <td>21</td>\n",
       "      <td>16</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>十一</td>\n",
       "      <td>22</td>\n",
       "      <td>20</td>\n",
       "      <td>13</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>十一</td>\n",
       "      <td>27</td>\n",
       "      <td>16</td>\n",
       "      <td>15</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>十一</td>\n",
       "      <td>26</td>\n",
       "      <td>19</td>\n",
       "      <td>12</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>十一</td>\n",
       "      <td>22</td>\n",
       "      <td>15</td>\n",
       "      <td>10</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  period  线路A  线路B  线路C  线路D\n",
       "0     五一   31   22   14    8\n",
       "1     五一   29   23   16    4\n",
       "2     五一   32   26   20    6\n",
       "3     五一   30   25   15    5\n",
       "4     五一   30   24   18    5\n",
       "5     十一   25   21   16    5\n",
       "6     十一   22   20   13    7\n",
       "7     十一   27   16   15    8\n",
       "8     十一   26   19   12    7\n",
       "9     十一   22   15   10    8"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据清洗，转换表\n",
    "tourist_df_n = tourist_df.reset_index()\n",
    "\n",
    "# 重命名黄金周变量为period，原变量名为index\n",
    "tourist_df_n = tourist_df_n.rename(index=str , columns = {'index':'period'})\n",
    "\n",
    "# 填充原始数据中黄金周变量的缺失值\n",
    "tourist_df_n.loc[0:5 , 'period'] = '五一'\n",
    "tourist_df_n.loc[6:10 , 'period'] = '十一'\n",
    "\n",
    "tourist_df_n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>period</th>\n",
       "      <th>plan</th>\n",
       "      <th>amount</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>五一</td>\n",
       "      <td>线路A</td>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>五一</td>\n",
       "      <td>线路A</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>五一</td>\n",
       "      <td>线路A</td>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>五一</td>\n",
       "      <td>线路A</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>五一</td>\n",
       "      <td>线路A</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>十一</td>\n",
       "      <td>线路A</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>十一</td>\n",
       "      <td>线路A</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>十一</td>\n",
       "      <td>线路A</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>十一</td>\n",
       "      <td>线路A</td>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>十一</td>\n",
       "      <td>线路A</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>五一</td>\n",
       "      <td>线路B</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>五一</td>\n",
       "      <td>线路B</td>\n",
       "      <td>23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>五一</td>\n",
       "      <td>线路B</td>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>五一</td>\n",
       "      <td>线路B</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>五一</td>\n",
       "      <td>线路B</td>\n",
       "      <td>24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>十一</td>\n",
       "      <td>线路B</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>十一</td>\n",
       "      <td>线路B</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>十一</td>\n",
       "      <td>线路B</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>十一</td>\n",
       "      <td>线路B</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>十一</td>\n",
       "      <td>线路B</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>五一</td>\n",
       "      <td>线路C</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>五一</td>\n",
       "      <td>线路C</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>五一</td>\n",
       "      <td>线路C</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>五一</td>\n",
       "      <td>线路C</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>五一</td>\n",
       "      <td>线路C</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>十一</td>\n",
       "      <td>线路C</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>十一</td>\n",
       "      <td>线路C</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>十一</td>\n",
       "      <td>线路C</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>十一</td>\n",
       "      <td>线路C</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>十一</td>\n",
       "      <td>线路C</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>五一</td>\n",
       "      <td>线路D</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>五一</td>\n",
       "      <td>线路D</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>五一</td>\n",
       "      <td>线路D</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>五一</td>\n",
       "      <td>线路D</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>五一</td>\n",
       "      <td>线路D</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>十一</td>\n",
       "      <td>线路D</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>十一</td>\n",
       "      <td>线路D</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>十一</td>\n",
       "      <td>线路D</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>十一</td>\n",
       "      <td>线路D</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>十一</td>\n",
       "      <td>线路D</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   period plan  amount\n",
       "0      五一  线路A      31\n",
       "1      五一  线路A      29\n",
       "2      五一  线路A      32\n",
       "3      五一  线路A      30\n",
       "4      五一  线路A      30\n",
       "5      十一  线路A      25\n",
       "6      十一  线路A      22\n",
       "7      十一  线路A      27\n",
       "8      十一  线路A      26\n",
       "9      十一  线路A      22\n",
       "10     五一  线路B      22\n",
       "11     五一  线路B      23\n",
       "12     五一  线路B      26\n",
       "13     五一  线路B      25\n",
       "14     五一  线路B      24\n",
       "15     十一  线路B      21\n",
       "16     十一  线路B      20\n",
       "17     十一  线路B      16\n",
       "18     十一  线路B      19\n",
       "19     十一  线路B      15\n",
       "20     五一  线路C      14\n",
       "21     五一  线路C      16\n",
       "22     五一  线路C      20\n",
       "23     五一  线路C      15\n",
       "24     五一  线路C      18\n",
       "25     十一  线路C      16\n",
       "26     十一  线路C      13\n",
       "27     十一  线路C      15\n",
       "28     十一  线路C      12\n",
       "29     十一  线路C      10\n",
       "30     五一  线路D       8\n",
       "31     五一  线路D       4\n",
       "32     五一  线路D       6\n",
       "33     五一  线路D       5\n",
       "34     五一  线路D       5\n",
       "35     十一  线路D       5\n",
       "36     十一  线路D       7\n",
       "37     十一  线路D       8\n",
       "38     十一  线路D       7\n",
       "39     十一  线路D       8"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 转换长表\n",
    "tourist_df_long = tourist_df_n.melt(id_vars='period' , var_name='plan' , value_name='amount')\n",
    "tourist_df_long"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>df</th>\n",
       "      <th>sum_sq</th>\n",
       "      <th>mean_sq</th>\n",
       "      <th>F</th>\n",
       "      <th>PR(&gt;F)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>C(period)</th>\n",
       "      <td>1.0</td>\n",
       "      <td>119.025</td>\n",
       "      <td>119.025000</td>\n",
       "      <td>30.617363</td>\n",
       "      <td>4.208408e-06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C(plan)</th>\n",
       "      <td>3.0</td>\n",
       "      <td>2431.475</td>\n",
       "      <td>810.491667</td>\n",
       "      <td>208.486602</td>\n",
       "      <td>4.475032e-21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C(period):C(plan)</th>\n",
       "      <td>3.0</td>\n",
       "      <td>88.875</td>\n",
       "      <td>29.625000</td>\n",
       "      <td>7.620579</td>\n",
       "      <td>5.564285e-04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Residual</th>\n",
       "      <td>32.0</td>\n",
       "      <td>124.400</td>\n",
       "      <td>3.887500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     df    sum_sq     mean_sq           F        PR(>F)\n",
       "C(period)           1.0   119.025  119.025000   30.617363  4.208408e-06\n",
       "C(plan)             3.0  2431.475  810.491667  208.486602  4.475032e-21\n",
       "C(period):C(plan)   3.0    88.875   29.625000    7.620579  5.564285e-04\n",
       "Residual           32.0   124.400    3.887500         NaN           NaN"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tourist_lm = ols('amount~C(period)+C(plan)+C(period):C(plan)' , data=tourist_df_long).fit()\n",
    "sm.stats.anova_lm(tourist_lm) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "结论\n",
    "\n",
    "对于period因素，4.208408e-06 < 0.05 ， 拒绝原假设，即不同时期有显著影响\n",
    "\n",
    "对于plan因素，4.475032e-21 < 0.05 ， 拒绝原假设，即不同旅游路线有显著影响\n",
    "\n",
    "对于交互因素，5.564285e-04 < 0.05 ， 拒绝原假设，旅游线路与黄金周的交互作用对游客人数产生显著影响（旅游景点具有一定季节性特征，两者相互作用影响游客人数）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
